<template>
  <div>
    <el-table
      ref="tableRef"
      v-loading="tableOptions.loading"
      :border="true"
      stripe
      :data="tableOptions.dataList"
      :height="tableOptions.height"
      :cell-style="tableStyle.cellStyle"
      :header-cell-style="tableStyle.headerCellStyle"
      @sort-change="handleSortChange"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
    >
      <el-table-column
        align="center"
        show-overflow-tooltip
        type="selection"
        width="50"
      />
      <el-table-column label="序号" type="index" width="55">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="标题" prop="title" width="420">
        <template #default="{ row }">
          <el-link
            :href="'./#/cmsdetail?type=notice&id=' + row.id"
            target="_blank"
            type="primary"
          >
            {{ row.title }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="发送人"
        prop="createUserName"
        width="120"
      />
      <el-table-column
        align="center"
        label="状态"
        prop="statusDesc"
        width="80"
      />
      <el-table-column
        align="center"
        :formatter="datetimeFormat"
        label="发送时间"
        prop="createTime"
        width="160"
      />
      <el-table-column
        align="center"
        :formatter="dateFormat"
        label="有效截止日期"
        prop="validEndTime"
        width="140"
      />
    </el-table>
    <el-pagination
      background
      :current-page="tableOptions.pagination.current"
      :layout="tableOptions.pagination.layout"
      :page-size="tableOptions.pagination.size"
      :total="tableOptions.pagination.total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>
<script setup lang="ts">
import { dateFormat, datetimeFormat } from "@/utils/common";
import { queryNoticeTable } from "@/api/admin/message/notice/";
import { BasicTableProps, useTable } from "@/hooks/table";

const tableRef = ref(null);

const tableOptions: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {
    status: "0"
  },
  autoLoad: true,
  queryFunction: queryNoticeTable,
  descs: ["create_time"]
});

const {
  tableStyle,
  queryTableData,
  handleSelectionChange,
  handleResetQuery,
  handleRowClick,
  handleSizeChange,
  handleCurrentChange,
  handleSortChange,
  handleToggleHeight
} = useTable(tableOptions, tableRef);

onMounted(() => {
  handleToggleHeight(280);
});

defineExpose({ queryTableData });
</script>
